package com.qf.edu.controller;

import com.qf.edu.entity.Users;
import com.qf.edu.service.PassWordService;
import com.qf.edu.service.impl.PassWordServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:修改密码控制
 * @datatime: 2022/5/26 17:26
 */
@WebServlet("/passWordCom")
public class PassWordCom extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String oldpassword = req.getParameter("oldPassword");
        String newpassword = req.getParameter("newPassword");
        String password = req.getParameter("password");
        // 获取session对象
        HttpSession session = req.getSession();
        Object password1 = session.getAttribute("password");

        if (password1.toString().equals(oldpassword)) {
            if (newpassword.equals(password)) {
                if (newpassword.length() >= 6) {
                    Object name = session.getAttribute("name");

                    Users users = new Users();
                    users.setPassword(newpassword);

                    PassWordService passWordService = new PassWordServiceImpl();
                    String s = passWordService.passWoSer(users, name.toString(), oldpassword);
                    System.out.println(s);
                    if (s.equals("修改密码通过")) {
                        //修改密码成功，就覆盖登录的原密码,不然查询个人信息会按登录的密码来查询
                        session.setAttribute("password",password);
                        System.out.println(name + "用户的密码已修改为：" + password);
                        resp.getWriter().println("修改密码已通过<br>");
                    } else {
                        System.out.println(s);
                        resp.getWriter().println(s + "<br>");
                    }
                } else {
                    System.out.println("密码长度需6位或以上");
                    resp.getWriter().println("密码长度需6位或以上，请重新输入<br>");
                }
            } else {
                System.out.println("确认密码不一致");
                resp.getWriter().println("新密码与确认密码不一致，请重新输入<br>");
            }
        } else {
            System.out.println("您原密码输入有误，请重新输入");
            resp.getWriter().println("您原密码输入有误，请重新输入<br>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
